package rjgc.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import rjgc.mall.common.MessageConstant;
import rjgc.mall.common.MySessionContext;
import rjgc.mall.common.Result;

import rjgc.mall.entity.Seller;
import rjgc.mall.entity.User;
import rjgc.mall.entity.param.BuyParam;
import rjgc.mall.entity.vo.*;
import rjgc.mall.mapper.*;
import rjgc.mall.service.UserService;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RentuserMapper rentuserMapper;
    @Autowired
    private ExpenseMapper expenseMapper;
    @Autowired
    private UMapper uMapper;
    @Autowired
    private RentMapper rentMapper;
    @Autowired
    private MedicMapper medicMapper;
    @Autowired
    private AdviceMapper adviceMapper;
    @Autowired
    private UserService userService;
    @Autowired
    HttpServletRequest request;

    /**
     * 用户购买时填写用户信息 升级包A 没有购买意向人了 购买后减去库存
     * 最后返回
     * @param buyParam
     * user       用户姓名和电话号码
     * tradeTime  交易时间 前端传进来的形式 : yyyy-MM-dd hh-mm-ss
     * tradePlace 交易地点
     * goodsId    商品编号
     * @return
     */
    @RequestMapping("/buy")
    public Result buy(@RequestHeader("userToken") String userToken,@RequestBody BuyParam buyParam) {
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        User user=(User) session.getAttribute("user");
        userService.buy3( user.getId(),buyParam.getTradeTime(), buyParam.getTradePlace(),buyParam.getGoodsId());
        return new Result(true, MessageConstant.GOODS_BUY_SUCCESS);
    }

    /**
     * 用户注册功能
     * @param userRegisterParam 姓名name 账号account 电话tele 默认交易地点defaultTradePlace 密码password
     * @return
     */
    @RequestMapping("/register")
    public Result register(@RequestBody UserRegisterParam userRegisterParam){
        userService.registered(userRegisterParam);
        return new Result(true,MessageConstant.USER_REGISTERED_SUCCESS);
    }

    /**
     * 用户登录
     * @param account 账号
     * @param password 密码
     * @return
     */
    @RequestMapping("/login")
    public Result login(String account,String password){
        System.out.println(account);
        User user = userService.login(account,password);
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        MySessionContext myContext = MySessionContext.getInstance();
        myContext.AddSession(session);
        return new Result(true,MessageConstant.USER_LOGIN_SUCCESS,session.getId());
    }
    //退出系统
    @RequestMapping("/logout")
    public Result logout(@RequestHeader("token") String token){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(token);
        myContext.DelSession(session);
        return new Result(true,MessageConstant.LOGOUT_SUCCESS);
    }

    //获取登录用户姓名
    @RequestMapping("/getLoginUser")
    public Result getLoginUser(@RequestHeader("token") String token){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(token);
        User user=(User) session.getAttribute("user");
        return new Result(true,MessageConstant.GET_USER_SUCCESS,user);
    }

    /**
     * 修改信息
     * @param user
     * @return
     */
    @RequestMapping("/updateInfo")
    public Result updateInfo(@RequestHeader("userToken") String userToken,@RequestBody User user){
        userService.updateInfo(user);
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        session.setAttribute("user",user);
        return new Result(true,MessageConstant.USER_UPDATE_SUCCESS);
    }

    /**
     * 获取登录成功后用户的信息
     * @param userToken
     * @return
     */
    @RequestMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader("userToken") String userToken){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        User user=(User) session.getAttribute("user");
        return new Result(true,"获取登录用户信息成功",user);
    }

    //新的购买商品
    @RequestMapping("/buy2")
    public Result buy2(@RequestHeader("userToken") String userToken, @RequestBody BuyParam buyParam, @RequestParam int num) {
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(userToken);
        User user=(User) session.getAttribute("user");
        userService.buy4( user.getId(),buyParam.getTradeTime(), buyParam.getTradePlace(),buyParam.getGoodsId(),num);
        return new Result(true, MessageConstant.GOODS_BUY_SUCCESS);
    }

    //发布意见
    @RequestMapping("/advice")
    public Result advice(String title,String content,String reply){
        List<advice> adviceList = adviceMapper.selectList(null);
        int id=adviceList.size()+1;
        advice ad= new advice(id,title,content,reply);
        adviceMapper.insert(ad);
        return new Result<>(true,"发布成功");
    }
    //撤回意见
    @RequestMapping("/removeadvice")
    public Result removeadvice(Integer id){
        QueryWrapper<advice> queryWrapper=new QueryWrapper<advice>();
        queryWrapper.eq("id",id);
        adviceMapper.delete(queryWrapper);
        return new Result<>(true,"删除成功");
    }
    //展示意见
    @RequestMapping("/showadvice")
    public Result showadvice() {
        List<advice> adviceList = adviceMapper.selectList(null);
        return new Result<>(true, "查看成功", adviceList);
    }
    //根据id查询意见
    @RequestMapping("/checkadvicebyid")
    public Result checkadvicebyid(Integer id){
        advice a=adviceMapper.selectById(id);
        return new Result<>(true,"查看成功",a);
    }
    //回复意见
    @RequestMapping("/replybyid")
    public Result replybyid(Integer id,String reply){
        advice ad=adviceMapper.selectById(id);
        ad.setReply(reply);
        adviceMapper.updateById(ad);
        return new Result<>(true,"回复成功",ad);
    }
    //发布租房信息
//    @RequestMapping("/rent")
//    public Result rent(String name, String place, Integer area, Integer vx, Integer price, String description, @RequestParam("img") MultipartFile img)throws IOException {
//        List<rent> rentList = rentMapper.selectList(null);
//        BASE64Encoder base64Encoder = new BASE64Encoder();
//        String encode = base64Encoder.encode(img.getBytes());
//        int id=rentList.size()+1;
//        Integer status=1;
//        rent r=new rent(id,name,place,area,vx,price,description,status,);
//        rentMapper.insert(r);
//        return new Result<>(true,"发布成功");
//    }
    @RequestMapping("/rent")
    public Result rent(String name, String place, Integer area, Integer vx, Integer price, String description,String img) {
        List<rent> rentList = rentMapper.selectList(null);
//        BASE64Encoder base64Encoder = new BASE64Encoder();
//        String encode = base64Encoder.encode(img.getBycdcdtes());
        int id=rentList.size()+1;
        Integer status=1;
        rent r=new rent(id,name,place,area,vx,price,description,status,img);
        rentMapper.insert(r);
        return new Result<>(true,"发布成功");
    }
    //展示组房信息
    @RequestMapping("/showrent")
    public Result showrent(){
        List<rent> rentList = rentMapper.selectList(null);
        return new Result<>(true,"查看成功",rentList);
    }
    //根据id展示组房信息
    @RequestMapping("/rentbyid")
    public Result rentbyid(Integer id){
        rent r=rentMapper.selectById(id);
        return new Result<>(true,"查看成功",r);
    }
//    用户申请服务
    @RequestMapping("/medic")
    public Result medic(String name, String place, Integer type, String time, String content,Integer doc){
        List<medic> medicList = medicMapper.selectList(null);
        int id=medicList.size()+1;
        Integer state=1;
        medic m=new medic(id,name,place,type,time,content,state,doc);
        medicMapper.insert(m);
        return new Result<>(true,"发布成功");
    }
    //展示医疗申请
    @RequestMapping("/showmedic")
    public Result showmedic(){
        List<medic> medicList = medicMapper.selectList(null);
        return new Result<>(true,"查看成功",medicList);
    }
    @RequestMapping("/showmedicbyid")
    public Result showmedicbyid(Integer id){
        medic me=medicMapper.selectById(id);
        return new Result<>(true,"查看成功",me);
    }
    @RequestMapping("/rentuser")
    public Result rentuser(String name, String account, String password, String telephone, String place) {
        List<rentuser> rentuserList = rentuserMapper.selectList(null);
        int id=rentuserList.size()+1;
        Integer status=2;
        rentuser rus=new rentuser(id,name,account,password,telephone,place,status);
        rentuserMapper.insert(rus);
        return new Result<>(true,"注册成功",rus);
    }

    //租客登录
    @RequestMapping("/rentlogin")
    public Result rentlogin(String account, String password) {
        QueryWrapper<rentuser> queryWrapper=new QueryWrapper<rentuser>();
        queryWrapper.eq("account",account);
        rentuser ru=rentuserMapper.selectOne(queryWrapper);
        if (ru==null)
            return new Result(false,"不存在");
        else if (!ru.getPassword().equals(password)) {
            return new Result(false,"密码错误");
        } else
            return new Result(true,"登录成功",ru);
    }
    //确认缴费信息
    @RequestMapping("/hexpense")
    public Result hexpense(Integer id,Integer uid,Integer carid,String uname,String cartype,String carnum) {
        List<expense> exList = expenseMapper.selectList(null);
        Integer hstatue=1;
        Integer cstatue=1;
        expense e=new expense(id,uid,carid,uname,hstatue,cstatue,cartype,carnum);
        expenseMapper.insert(e);
        return new Result<>(true,"发布成功",e);
    }
    //根据id展示缴费信息
    @RequestMapping("/showhexpense")
    public Result showhexpense(Integer id){
        QueryWrapper<expense> queryWrapper=new QueryWrapper<expense>();
        queryWrapper.eq("id",id);
        expense eh=expenseMapper.selectOne(queryWrapper);
        return new Result<>(true,"查看成功",eh);
    }
   //绑定车位
   @RequestMapping("/bindingcar")
   public Result bindingcar(Integer id,String cartype,String carnum){
       expense eh=expenseMapper.selectById(id);
       eh.setCartype(cartype);
       eh.setCarnum(carnum);
       expenseMapper.updateById(eh);
       return new Result<>(true,"绑定成功",eh);
   }
}

